﻿using Common.Library;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using webslice;
using System.ComponentModel;

namespace OnlineVideos.ViewModels
{
    public static class CreateCustomImage
    {
       static string fullpath = string.Empty;
       static Page Navigationpurpose = default(Page);
        public static void createFrontImage(string path,Page currentpage)
        {
            Navigationpurpose = currentpage;
            fullpath = path;
            try
            {
                BitmapImage bi = new BitmapImage(new Uri(App.webinfo.PivotImage, UriKind.RelativeOrAbsolute));
                bi.CreateOptions = BitmapCreateOptions.None;
                bi.ImageOpened += bi_ImageOpened;
            }
            catch (Exception ex)
            {
                Constants.ImageUrls.Clear();
                CreateImage();
            }
         
         
        }
        public static void CreateImage(BitmapImage bi = null)
        {
            try
            {
               
                if (!Storage.DirectoryExists("/Images"))
                {
                    Storage.CreateDirectory("/Images");
                }

                Grid grid = new Grid();
                grid.Background = new SolidColorBrush(Colors.LightGray);
                Canvas MainCanvas = new Canvas();
                MainCanvas.Background = new SolidColorBrush(Colors.LightGray);
                MainCanvas.Width = 130;
                MainCanvas.Height = 190;


                TextBlock TitleText = new TextBlock();
                TitleText.Text = (Constants.WebSliceTitle.Length > 10) ? Constants.WebSliceTitle.Substring(0, 10) + ".." : Constants.WebSliceTitle;
                TitleText.TextTrimming = TextTrimming.WordEllipsis;
                TitleText.FontFamily = new FontFamily("Segoe WP Semibold");
                TitleText.Foreground = new SolidColorBrush(Colors.Gray);
                TitleText.Margin = new Thickness(10,2, 0, 0);
                TitleText.FontSize = 24;
                TitleText.SetValue(Canvas.TopProperty, (double)-5);
                TitleText.Width = 120;

                BitmapImage ProductBitmap = new BitmapImage();
                ProductBitmap.CreateOptions = BitmapCreateOptions.None;
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile("myImage.jpg", FileMode.Open, FileAccess.Read))
                    {
                        ProductBitmap.SetSource(local);
                    }
                }
                Image ProductImage = new Image();
                ProductImage.SetValue(Canvas.TopProperty, (double)32);
                ProductImage.Source = ProductBitmap;
                Image FavImage = new Image();
                if (bi!= null)
                {
                    
                    FavImage.Height = 16;
                    FavImage.Width = 16;
                    FavImage.Source = bi as ImageSource;
                    FavImage.SetValue(Canvas.LeftProperty, (double)105);
                    FavImage.SetValue(Canvas.TopProperty, (double)40);

                }
                TextBlock SelectedText = new TextBlock();
                SelectedText.Text = (App.webinfotable.SelectedText.Length > 10) ? App.webinfotable.SelectedText.Substring(0, 10) + ".." : App.webinfotable.SelectedText;
                SelectedText.TextTrimming = TextTrimming.WordEllipsis;
                SelectedText.Margin = new Thickness(10, 0, 0,2);
                SelectedText.FontFamily = new FontFamily("Segoe WP Semibold");
                SelectedText.Foreground = new SolidColorBrush(Colors.Gray);
                SelectedText.FontSize = 22;
                SelectedText.SetValue(Canvas.TopProperty, (double)158);
                SelectedText.Width = 120;

                MainCanvas.Children.Add(TitleText);
                MainCanvas.Children.Add(ProductImage);
                if(bi!=null)
                MainCanvas.Children.Add(FavImage);
                MainCanvas.Children.Add(SelectedText);
                grid.Children.Add(MainCanvas);


                WriteableBitmap wbmp = new WriteableBitmap(grid, null);
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var local = myIsolatedStorage.OpenFile(fullpath, System.IO.FileMode.OpenOrCreate))
                    {
                        wbmp.SaveJpeg(local, 130, 190, 0, 100);
                        local.Close();
                        local.Dispose();
                        myIsolatedStorage.Dispose();
                    }
                }
                
                    Constants.WebSliceImagePath = fullpath;
                    Navigationpurpose.NavigationService.GoBack();
                

            }
            catch (Exception ex)
            {

            }
        }
        static void bi_ImageOpened(object sender, RoutedEventArgs e)
        {
            CreateImage(sender as BitmapImage);
            
        }
    }
}
